### texreg method for multinom objects

extract.multinom <- function(model,
                             include.pvalues = TRUE,
                             include.aic = TRUE,
                             include.bic = TRUE,
                             include.loglik = TRUE,
                             include.deviance = TRUE,
                             include.nobs = TRUE,
                             include.groups = TRUE,
                             include.lrtest = TRUE,
                             include.divided = TRUE,
                             levels = model$lev,
                             beside = FALSE,
                             ...) {
  
  s <- summary(model, ...)
  
  coefnames <- model$coefnames
  co <- s$coefficients
  se <- s$standard.errors
  
  ddpcr::quiet(lr.test <- lmtest::lrtest(model))
  lr.stat <- lr.test$C[2]
  lr.p <- lr.test$P[2]
  
  ddpcr::quiet(lr.test.div <- lmtest::lrtest(model, .~. - dividedpressenate - dividedpressenate:zpolarization - 
                                               dividedpressenate:zpressendist - dividedpressenate:zpresfildist - 
                                               dividedpressenate:zprescommdist - dividedpressenate:zpreschairdist))
  lr.div.stat <- lr.test.div$C[2]
  lr.div.p <- lr.test.div$P[2]
  
  
  if (!"matrix" %in% class(co)) {
    co <- t(as.matrix(co))
    se <- t(as.matrix(se))
  }
  
  gof <- numeric()
  gof.names <- character()
  gof.decimal <- logical()
  if (include.aic == TRUE) {
    aic <- AIC(model)
    gof <- c(gof, aic)
    gof.names <- c(gof.names, "AIC")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.bic == TRUE) {
    bic <- BIC(model)
    gof <- c(gof, bic)
    gof.names <- c(gof.names, "BIC")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.loglik == TRUE) {
    lik <- logLik(model)[1]
    gof <- c(gof, lik)
    gof.names <- c(gof.names, "Log Likelihood")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.deviance == TRUE) {
    dev <- deviance(model)
    gof <- c(gof, dev)
    gof.names <- c(gof.names, "Deviance")
    gof.decimal <- c(gof.decimal, TRUE)
  }
  if (include.lrtest == TRUE) {
    lrstat <- lr.stat
    lrp <- lr.p
    gof <- c(gof, lrstat, lrp)
    gof.names <- c(gof.names, "Likelihood Ratio Test", "%Likelihood Ratio Test p-value")
    gof.decimal <- c(gof.decimal, TRUE, TRUE)
  }

  if (include.divided == TRUE) {
    lrstat.div <- lr.div.stat
    lrp.div <- lr.div.p
    gof <- c(gof, lrstat.div, lrp.div)
    gof.names <- c(gof.names, "Likelihood Ratio Test of Significance of Divided Government", "%Likelihood Ratio Test of Significance of Divided Government p-value")
    gof.decimal <- c(gof.decimal, TRUE, TRUE)
  }
  
    if (include.nobs == TRUE) {
    n <- nrow(s$fitted.values)
    gof <- c(gof, n)
    gof.names <- c(gof.names, "Num. obs.")
    gof.decimal <- c(gof.decimal, FALSE)
  }
  if (include.groups == TRUE) {
    K <- ncol(model$residuals)
    if (K == 1) {
      K <- 2
    }
    gof <- c(gof, K)
    gof.names <- c(gof.names, "K")
    gof.decimal <- c(gof.decimal, FALSE)
  }
  
  if (is.null(rownames(co))) {
    if (include.pvalues == TRUE) {
      zval <- co[1, ] / se[1, ]
      pval <- 2 * pnorm(abs(zval), lower.tail = FALSE)
    } else {
      pval <- numeric(0)
    }
    tr <- createTexreg(
      coef.names = coefnames,
      coef = co[1, ],
      se = se[1, ],
      pvalues = pval,
      gof.names = gof.names,
      gof = gof,
      gof.decimal = gof.decimal
    )
    return(tr)
  } else if (beside == TRUE) {
    trlist <- list()
    for (i in which(rownames(co) %in% levels)) {
      if (include.pvalues == TRUE) {
        zval <- co[i, ] / se[i, ]
        pval <- 2 * pnorm(abs(zval), lower.tail = FALSE)
      } else {
        pval <- numeric(0)
      }
      
      tr <- createTexreg(
        coef.names = coefnames,
        coef = co[i, ],
        se = se[i, ],
        pvalues = pval,
        gof.names = gof.names,
        gof = gof,
        gof.decimal = gof.decimal,
        model.name = rownames(co)[i]
      )
      
      trlist <- c(trlist, tr)
    }
    if (length(trlist) == 1) {
      return(trlist[[1]])
    } else {
      return(trlist)
    }
  } else {
    pval <- numeric()
    stderr <- numeric()
    coefs <- numeric()
    nm <- character()
    for (i in which(rownames(co) %in% levels)) {
      nm <- c(nm, paste0(rownames(co)[i], ": ", coefnames))
      coefs <- c(coefs, co[i, ])
      stderr <- c(stderr, se[i, ])
      if (include.pvalues == TRUE) {
        zval <- co[i, ] / se[i, ]
        pval <- c(pval, 2 * pnorm(abs(zval), lower.tail = FALSE))
      } else {
        pval <- numeric(0)
      }
    }
    tr <- createTexreg(
      coef.names = nm,
      coef = coefs,
      se = stderr,
      pvalues = pval,
      gof.names = gof.names,
      gof = gof,
      gof.decimal = gof.decimal
    )
    return(tr)
  }
}